package controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.User;

@Controller
public class UserController {
	@ResponseBody
	@RequestMapping(value={"/login"},method=RequestMethod.GET,headers="action=l",params="p=1")
	public User login(@RequestParam(value="uname") String uname){
		System.out.println("do login..."+uname);
		return new User(1,uname,100);
	}
	@ResponseBody
	@RequestMapping(value={"/login"},method=RequestMethod.POST)
	public User login1(User u){
		System.out.println("do login...");
		return u;
	}
	@ResponseBody
	@RequestMapping(value={"/users/{uid}/books/{bid}"},method=RequestMethod.POST)
	public User loadUser(@PathVariable(value="uid") int uid,@PathVariable(value="bid") int bid){
		System.out.println(uid+"....."+bid);
		return new User(uid,"john",123);
	}
	
	@ResponseBody
	@RequestMapping(value={"/users"},method=RequestMethod.POST,headers="Content-Type=application/json")
	public User addUser(@RequestBody User u){
		
		return u;
	}
	@RequestMapping(value="/validate",method=RequestMethod.GET)
	public void checkCode(OutputStream out,HttpSession sen) throws Exception{
		String path=sen.getServletContext().getRealPath("/download/");
		
		
		InputStream in=new FileInputStream(path+"jeep1.jpeg");
		byte[] buf=new byte[256];
		int len=0;
		while((len=in.read(buf))!=-1){
			out.write(buf,0,len);
		}
		in.close();
		out.close();
		
	}
	
	

}
